﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class NoticeDAL
    {
        public static int Save(NoticeInfo info)
        {
            string sql = string.Format("insert into [notice]([UserID],[Title],[Context],[IsMust],[ToUsers],[HasRead],[CreateDate]) values({0},'{1}','{2}','{3}','{4}','{5}','{6}');select @@identity;", info.UserID, info.Title, info.Context, info.IsMust, info.ToUsers, info.HasRead, info.CreateDate);
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql));
        }
        public static int Update(NoticeInfo info)
        {
            string sql = string.Format("update [notice] set [Title]='{0}',[Context]='{1}',[IsMust]='{2}',[ToUsers]='{3}' where [ID]={4}", info.Title, info.Context, info.IsMust, info.ToUsers, info.ID);
            return CommonDAL.ExecuteNoneQuery(sql) ? info.ID : 0;
        }
        public static bool Read_Notice(int id,int userid)
        {
            string sql = string.Format("update [notice] set [HasRead]=case when [HasRead]='' then convert(varchar(10),{0}) else [HasRead]+','+convert(varchar(10),{0}) end where [ID]={1}", userid, id);
            return CommonDAL.ExecuteNoneQuery(sql);
        }
        public static NoticeInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[notice]");
            return Simple_Data(dr);
        }
        public static int FindNoReadCount(int userid)
        {
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(string.Format("select count(1) from [notice] where charindex(','+convert(varchar(20), {0})+',', ','+ToUsers+',') > 0 and charindex(','+convert(varchar(20), {0})+',', ','+HasRead+',') = 0", userid)));
        }
        public static List<NoticeInfo> FindPage(string keyword, int userid, string begindate,string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [notice].Title like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [notice].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[notice]", out count);
            List<NoticeInfo> list = new List<NoticeInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(All_Data(row));
            }
            return list;
        }
        
        private static NoticeInfo Simple_Data(DataRow dr)
        {
            NoticeInfo info = new NoticeInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Title = DataConvert.ToString(dr["Title"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.IsMust = DataConvert.ToBool(dr["IsMust"]);
                info.ToUsers = DataConvert.ToString(dr["ToUsers"]);
                info.HasRead = DataConvert.ToString(dr["HasRead"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }

        private static NoticeInfo All_Data(DataRow dr)
        {
            NoticeInfo info = new NoticeInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Title = DataConvert.ToString(dr["Title"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.IsMust = DataConvert.ToBool(dr["IsMust"]);
                info.ToUsers = DataConvert.ToString(dr["ToUsers"]);
                info.HasRead = DataConvert.ToString(dr["HasRead"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);

                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.HeadImg = DataConvert.ToString(dr["HeadImg"]);
                info.HeadImg = Commons.domain + (info.HeadImg == "" ? "/system/img/head.png" : info.HeadImg);
            }
            return info;
        }
    }
}
